<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="设备" prop="equipmentId">
        <el-select v-model="queryParams.equipmentId" placeholder="请选择设备" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="item in equipmentList" :label="item.equipmentName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="借用开始时间" prop="startTime">
        <el-date-picker clearable
                        v-model="queryParams.startTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择借用开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="借用结束时间" prop="endTime">
        <el-date-picker clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择借用结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="borrowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="设备名称" align="center" prop="equipmentName"/>
      <el-table-column label="用户姓名" align="center" prop="userName"/>
      <el-table-column label="借用开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借用结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借用状态" align="center" prop="statusDesc"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDelete(scope.row)"
          >器材归还
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {getBorrow, delBorrow, addBorrow, updateBorrow, listBorrowMe, returnBorrow} from "@/api/system/borrow";
import {listEquipment} from "@/api/system/equipment";
import {listByRole} from "@/api/system/user";

export default {
  name: "Borrow",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实验设备借用表格数据
      borrowList: [],
      equipmentList:[],
      instructorList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentId: null,
        userId: null,
        startTime: null,
        endTime: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startTime: [
          {required: true, message: "借用开始时间不能为空", trigger: "blur"}
        ],
        endTime: [
          {required: true, message: "借用结束时间不能为空", trigger: "blur"}
        ],
        equipmentId: [
          {required: true, message: "借用状态不能为空", trigger: "change"}
        ],
        userId: [
          {required: true, message: "借用状态不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getEquipmentList();
    this.getInstructorList();
  },
  methods: {
    /** 查询实验设备借用列表 */
    getList() {
      this.loading = true;
      listBorrowMe(this.queryParams).then(response => {
        this.borrowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getInstructorList() {
      listByRole({
      }).then(response => {
        this.instructorList = response.data;
      });
    },
    getEquipmentList() {
      listEquipment().then(response => {
        this.equipmentList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentId: null,
        userId: null,
        startTime: null,
        endTime: null,
        status: null,
        createdAt: null,
        updatedAt: null,
        createdBy: null,
        updatedBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认已归还该实验设备').then(function () {
        return returnBorrow(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("归还成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/borrow/export', {
        ...this.queryParams
      }, `borrow_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
